<?php

namespace apiend\modules\book\models;

use Yii;
use yii\db\Exception;
use common\helpers\Url;
use yii\helpers\Json;
use common\helpers\ArrayHelper;
use common\models\book\Book;
use common\models\book\BookStage;
use common\models\book\BookCategory;
use common\models\book\BookWaiter;
use common\models\book\Company;
use common\models\book\CompanyDepartment;
use common\models\book\Stage;
use common\models\book\StageDate;
use common\models\book\RoomStage;
use common\models\book\Room;
use common\models\book\Category;

/**
 * 预约编辑模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BookAuditForm extends \common\base\FormModel {

    /**
     * @var string 数据原型模型
     */
    protected $modelClass = BookViewSearch::class;

    /**
     * @var string 对接人姓名
     */
    public $mark;

    /**
     * @var integer 资讯ID
     */
    public $allow = 1;

    /**
     * @return array validation rules
     */
    public function rules() {
        return [
            [['allow'], 'integer'],
            [['mark'], 'required'],
            [['id'], 'validateBook'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => '预约',
            'company_name' => '单位名称',
            'waiter_department_id' => '对接部门',
            'waiter_name' => '对接人',
            'contact' => '联系人',
            'contact_mobile' => '联系人手机号',
            'categorys' => '参观需求',
            'room_id' => '场地需求',
            'date' => '参观日期',
            'stages' => '参观时段',
        ];
    }

    /**
     * @param string $attribute the attribute currently being validated
     */
    public function validateBook($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $model = $this->getModel();
        if (!$model || $model->isNewRecord) {
            return $this->addError($attribute, '预约信息不存在');
        }
        if (!$model->canAudit) {
            return $this->addError($attribute, '预约状态已变更，不能再进行审核');
        }
    }

    /**
     * 保存预约信息
     * @return Book
     */
    public function save() {
        if (!$this->validate()) {
            return $this->dumpError();
        }

        $model = $this->getModel();
        $model->setAttributes([
            'mark' => $this->mark,
        ]);
        if ($this->allow > 0) {
            $model->status = Book::STATUS_AUDITED;
        } elseif ($this->allow < 0) {
            $model->status = Book::STATUS_REJECT;
        }
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!$model->save()) {
                throw new Exception($model->getErrorMsg());
            }
            //todo 微信消息通知
            $transaction->commit();
        } catch (Exception $e) {
            // 记录回滚（事务回滚）
            $transaction->rollBack();
            return $this->dumpError($e->getMessage());
        }
        return $model;
    }

    /**
     * {@inheritdoc}
     */
    public function setExtraAttributes($model) {
        
    }

}
